package com.shinobi.timetracker;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.shinobi.timetracker.database.GlobalState;
import com.shinobi.timetracker.database.TimerResult;
import com.steema.teechart.DateTime;

/**
 * Created by IntelliJ IDEA. User: Semy Date: 09.05.11 Time: 20:30 To change
 * this template use File | Settings | File Templates.
 */
public class StartActivity extends Activity {

	private GlobalState globalState;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.start);

		this.globalState = (GlobalState) getApplication();

		InitializeStartButton();
		InitializeShowChartButton();
		InitializeSpinnerComboBox();

//		ExampleData();
	}

	public void ExampleData() {

		DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd.HH:mm");

		Date startTime;
		Date stopTime;
		TimerResult tr;
		String tag = "Praca";
		try {
			{
				startTime = (Date) formatter.parse("2011.05.17.22:00");
				stopTime = (Date) formatter.parse("2011.05.17.24:00");

				tr = new TimerResult(tag);
				tr.setStartTime(startTime.getTime());
				tr.setStopTime(stopTime.getTime());
				globalState.SaveResult(this, tr);
			}

			{
				startTime = (Date) formatter.parse("2011.05.18.18:00");
				stopTime = (Date) formatter.parse("2011.05.18.22:00");

				tr = new TimerResult(tag);
				tr.setStartTime(startTime.getTime());
				tr.setStopTime(stopTime.getTime());
				globalState.SaveResult(this, tr);
			}

			{
				startTime = (Date) formatter.parse("2011.05.12.08:00");
				stopTime = (Date) formatter.parse("2011.05.12.16:00");

				tr = new TimerResult(tag);
				tr.setStartTime(startTime.getTime());
				tr.setStopTime(stopTime.getTime());
				globalState.SaveResult(this, tr);
			}

			{
				startTime = (Date) formatter.parse("2011.05.10.10:00");
				stopTime = (Date) formatter.parse("2011.05.10.14:00");

				tr = new TimerResult(tag);
				tr.setStartTime(startTime.getTime());
				tr.setStopTime(stopTime.getTime());
				globalState.SaveResult(this, tr);
			}
		} catch (Exception exc) {
		}
	}

	/**
	 * Start button initialization.
	 */
	private void InitializeStartButton() {
		Button btn = (Button) findViewById(R.id.startButton);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				GlobalState state = (GlobalState) getApplication();
				Spinner s = (Spinner) findViewById(R.id.tagComboBox);
				String selectedTag = s.getSelectedItem().toString();
				state.StartTimer(selectedTag);
				startActivity(new Intent(StartActivity.this, StopActivity.class));
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		InitializeSpinnerComboBox();
	}

	/**
	 * Show Chart button initialization.
	 */
	private void InitializeShowChartButton() {
		Button showChartButton = (Button) findViewById(R.id.showChartButton);
		showChartButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				startActivity(new Intent(StartActivity.this,
						StatisticsActivity.class));
			}
		});
	}

	/**
	 * Spinner with tags initialization.
	 */
	private void InitializeSpinnerComboBox() {
		Spinner tagComboBox = (Spinner) findViewById(R.id.tagComboBox);
		ArrayList<String> tags = globalState.GetTags(this);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, tags);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tagComboBox.setAdapter(adapter);

		String defaultTag = globalState.CurrentTag();
		int position = adapter.getPosition(defaultTag);

		if (position < 0) {
			position = 0;
			tagComboBox.setSelection(position);
			globalState.SetCurrentTag((String) tagComboBox.getSelectedItem());
		} else {
			tagComboBox.setSelection(position);
		}

		tagComboBox.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parentView,
					View selectedItemView, int position, long id) {

				GlobalState globalState = (GlobalState) getApplication();
				Spinner tagComboBox = (Spinner) findViewById(R.id.tagComboBox);
				String tag = (String) tagComboBox.getSelectedItem();
				globalState.SetCurrentTag(tag);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				globalState.SetCurrentTag("");
			}

		});
	}
}